home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_brick3.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  3.7 KB  |  107 lines

  1. /*
  2.  * brick.sl -- Surface shader for a bricks.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a wall of bricks.  Need more be said?  OK.  It makes a good
  6.  *   looking staggered brick masonry.  It is especially convincing when
  7.  *   used in conjunction with the "brickbump" displacement shader (and
  8.  *   identical parameters).  Every other row of bricks is staggered.
  9.  *   The staggering isn't exact, however, and this variance is controlled
  10.  *   by the "rowvary" parameter.
  11.  * 
  12.  * PARAMETERS:
  13.  *    Ka, Kd            The usual
  14.  *    brickcolor, mortarcolor    Pretty obvious (default is red bricks)
  15.  *    brickvary                 How much does the brick color vary from
  16.  *                        brick to brick?
  17.  *    brickwidth                Width of a brick (in st space)
  18.  *    brickheight               Height of a brick (in st space)
  19.  *    mortarthickness           Thickness of the mortar (in st space)
  20.  *    rowvary                   How much does each row shift?
  21.  *    jagged                    How much do bricks deviate from squares?
  22.  *
  23.  * AUTHOR: written by Larry Gritz, 1992
  24.  *     This shader is very similar to (and based upon) brick shaders
  25.  *    by Darwyn Peachey.
  26.  *
  27.  * 
  28.  * HISTORY:
  29.  *      28 May 1992 -- written by lg for the "Timbre Trees" video (saucer)
  30.  *      12 Jan 1994 -- recoded by lg in correct shading language.
  31.  *
  32.  * last modified  12 Jan 1994 by Larry Gritz
  33.  */
  34.  
  35.  
  36.  
  37. surface
  38. k3d_brick3 ( float Ka = 1, Kd = 1;
  39.     color brickcolor = color "rgb" (.6,.1,.1);
  40.     color mortarcolor = color "rgb" (.6,.6,.6);
  41.         float jagged = 0.006, brickvary = 0.3;
  42.         float brickwidth = .25, brickheight = .08;
  43.         float mortarthickness = .01;
  44.         float rowvary = .25; )
  45. {
  46. #define BMWIDTH (brickwidth+mortarthickness)
  47. #define BMHEIGHT (brickheight+mortarthickness)
  48. #define MWF (mortarthickness*0.5/BMWIDTH)
  49. #define MHF (mortarthickness*0.5/BMHEIGHT)
  50. #define snoise(x) (2 * noise((x)) - 1)
  51. #define boxstep(a,b,x) (clamp(((x)-(a))/((b)-(a)),0,1))
  52. #define MINFILTERWIDTH 1.0e-7
  53.   color bcolor, Ct;
  54.   point PP2, Nf;
  55.   float sbrick, tbrick, w, h;
  56.   float scoord, tcoord, ss, tt;
  57.   float swidth, twidth;
  58.   float Nfactor;
  59.  
  60.   /* Determine how wide in s-t space one pixel projects to */
  61.   swidth = max (abs(Du(s)*du) + abs(Dv(s)*dv), MINFILTERWIDTH);
  62.   twidth = max (abs(Du(t)*du) + abs(Dv(t)*dv), MINFILTERWIDTH);
  63.  
  64.   Nf = faceforward (normalize(N),I);
  65.  
  66.   /* Make the shapes of the bricks vary just a bit */
  67.   PP2 = point noise (s/BMWIDTH, t/BMHEIGHT);
  68.   scoord = s + jagged * xcomp (PP2);
  69.   tcoord = t + jagged * ycomp (PP2);
  70.  
  71.   ss = scoord / BMWIDTH;   /* Determine which brick the point is in */
  72.   tt = tcoord / BMHEIGHT;  /*                   "                   */
  73.   swidth /= BMWIDTH;
  74.   twidth /= BMHEIGHT;
  75.  
  76.   /* shift alternate rows */
  77.   if (mod (tt*0.5, 1) > 0.5)
  78.       ss += 0.5;
  79.  
  80.   tbrick = floor (tt);   /* which brick row? */
  81.   /* Shift the columns randomly by row */
  82.   ss += rowvary * (noise (tbrick+0.5) - 0.5);
  83.  
  84.   sbrick = floor (ss);   /* which brick column? */
  85.   ss -= sbrick;          /* Now ss and tt are coords within the brick */
  86.   tt -= tbrick;
  87.  
  88.   /* Choose a color for the surface */
  89.   if (swidth >= 1)
  90.       w = 1 - 2*MWF;
  91.   else w = clamp (boxstep(MWF-swidth,MWF,ss), max(1-MWF/swidth,0), 1)
  92.      - clamp (boxstep(1-MWF-swidth,1-MWF,ss), 0, 2*MWF/swidth);
  93.  
  94.   if (twidth >= 1)
  95.       h = 1 - 2*MHF;
  96.   else h = clamp (boxstep(MHF-twidth,MHF,tt), max(1-MHF/twidth,0),1)
  97.      - clamp (boxstep(1-MHF-twidth,1-MHF,tt), 0, 2*MHF/twidth);
  98.  
  99.   /* Choose a brick color that varies from brick to brick */
  100.   bcolor = brickcolor * (1 + (brickvary * snoise (tbrick+(100*sbrick)+0.5)));
  101.  
  102.   Ct = mix (mortarcolor, bcolor, w*h);
  103.  
  104.   Oi = Os;
  105.   Ci = Os * Ct * (Ka * ambient() + Kd*diffuse(Nf));
  106. }
  107.